package corerun

import (
	"core/auth"
	"core/config"
	"core/coredto"
	"core/coreentity"
	"core/coreqto"
	"core/coreservice"
	"core/corestruct"
	"core/coreutil"

	"core/db"
	"core/server"
	"fmt"
	"go.uber.org/zap"
	"gorm.io/gorm"
	"os"
	"os/signal"
	"syscall"
)

func CoreStructToDb(db *gorm.DB, PrjName string) error {
	var m = clsdb{
		Ldb:     []coreentity.HtCls{},
		DB:      db,
		PrjName: PrjName,
	}
	return m.RefreshToDb()
}

func CoreActionToDb(ctx *corestruct.HttpContext, PrjName string) error {
	var m = actiondb{
		ctx: ctx,

		DB:      ctx.DB,
		PrjName: PrjName,
	}
	return m.RefreshToDb()
}
func CoreDbSetup() error {
	var m = &dbSetup{}
	return m.Setup()
}
func CoreInit(configfullname string) {
	config.Init(configfullname)
	coreutil.Init()

	CoreStructInit()

	coreentity.Init()
	coredto.Init()
	coreqto.Init()
	db.Init()
	coreservice.Init()
	auth.Init()

	server.HttpServerInit()

}
func CoreStar() {
	var svr = fmt.Sprintf("%s:%d", config.Config.Server.Ip, config.Config.Server.Port)
	coreutil.Logger.Info("开始监听", zap.String("Url", svr))
	go server.HttpEngine.Run(svr)

	// 接收到退出命令后做一些操作（kill -9 强杀命令不起作用)
	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit

	coreutil.Logger.Sync()

	//处理退出的逻辑
	fmt.Println("关闭server")
}
func CoreClose() {

}
